package com.zw.mes.module.shipments.controller;


import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import com.zw.mes.base.BaseController;
import com.zw.mes.common.JsonResult;
import com.zw.mes.entity.shipments.InstShipmentsPlanSheet;
import com.zw.mes.entity.shipments.InstShipmentsProductDetail;
import com.zw.mes.entity.shipments.InstShipmentsPurchasedDetail;
import com.zw.mes.module.cost.service.DictCommonService;
import com.zw.mes.module.prodtaskmanager.entity.VwProject;
import com.zw.mes.module.shipments.model.LinshiProductDetail;
import com.zw.mes.module.shipments.repository.PlanSheetRepository;
import com.zw.mes.module.shipments.service.InstShipmentsPlanEditService;
import com.zw.mes.module.shipments.service.InstShipmentsPlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.zw.mes.constant.Constant.GROUP_SPECTYPE;


/**
 * @Description: 创建发货计划
 * <p>
 * 权限：
 * @Author: liqingyu
 * @Date: 2020/11/10 08:58
 */
@Controller
@RequestMapping("/create/shipmentsEdit")
@SuppressWarnings("all")
public class InstShipmentsPlanEditController extends BaseController {


    @Autowired
    private DictCommonService dictCommonService;
    @Autowired
    private InstShipmentsPlanService instShipmentsPlanService;
    @Autowired
    private InstShipmentsPlanEditService instShipmentsPlanEditService;
    @Autowired
    private  PlanSheetRepository planSheetRepository;


    @GetMapping
//    @RequiresPermissions({"tec:proDetail:view"})
    public String page(Model model) {
        model.addAttribute("specialId", dictCommonService.findDictByGroupName(GROUP_SPECTYPE));
        return "shipments/InstShipmentsPlanEdit/page";
    }

    /**
     * 计划单主表
     */
    @ResponseBody
    @PostMapping("/list")
//    @RequiresPermissions({"report:query:view"})
    public JsonResult list(Model model,InstShipmentsPlanSheet instShipmentsPlanSheet) {
        startPage();
        List<InstShipmentsPlanSheet> list = instShipmentsPlanEditService.selectShipmentsList(instShipmentsPlanSheet);
        return jsonResult(list);
    }

    /**
     * 查询项目
     *
     * @param q
     * @return
     */
    @GetMapping("/project/list")
    @ResponseBody
    public List<VwProject> proList(String q) {
        return instShipmentsPlanService.findProjectByCode(q);
    }

    /**
     * 增加发货计划主单
     *
     * @param instShipmentsPlanSheet
     * @return
     */
    @ResponseBody
    @PostMapping("/edit/PlanSheet")
//    @RequiresPermissions({"tec:proProduc:edit"})
    public JsonResult editProduction(InstShipmentsPlanSheet instShipmentsPlanSheet) {
        instShipmentsPlanEditService.editInstShipmentsPlanSheet(instShipmentsPlanSheet);
        return jsonResult(1);
    }

    /**
     * 根据主表ID查询制作件明细表
     */
    @ResponseBody
    @PostMapping("/prodectDetail")
//    @RequiresPermissions({"report:query:view"})
    public JsonResult prodectDetail(Model model, InstShipmentsProductDetail instShipmentsProductDetail) {
        startPage();
        List<InstShipmentsProductDetail> list = instShipmentsPlanService.selectProductDetail(instShipmentsProductDetail);
        return jsonResult(list);
    }
//    /**
//     * 根据主表ID查询外购件明细
//     */
//    @ResponseBody
//    @PostMapping("/addPurch")
////    @RequiresPermissions({"report:query:view"})
//    public JsonResult PurchDetail(Model model, InstShipmentsProductDetail instShipmentsProductDetail) {
//        List<InstShipmentsPurchasedDetail> list = instShipmentsPlanService.selectPurchDetail(instShipmentsProductDetail);
//        return jsonResult(list);
//    }

    /**
     * 根据主表ID查询制作件明细表
     */
    @ResponseBody
    @PostMapping("/productDetailList")
    //    @RequiresPermissions({"report:query:view"})
    public JsonResult productDetailList(String proCode) {
        startPage();
        List<LinshiProductDetail> list = instShipmentsPlanService.selectLinshiProductDetail(proCode);
        return jsonResult(list);
    }

    /**
     * 保存制作件发货明细
     */
    @ResponseBody
    @PostMapping("/shipProduct")
    //    @RequiresPermissions({"report:query:view"})
    public JsonResult shipProduct(String arr, String sheetId) {
        Gson gson = new Gson();
        List<LinshiProductDetail> prodPlaDetList = gson.fromJson(arr, new TypeToken<List<LinshiProductDetail>>() {
        }.getType());
        instShipmentsPlanService.saveProductList(prodPlaDetList, sheetId);
        return jsonResult(1);
    }

    /**
     * 跳转选择制作件页面
     */
    @GetMapping("/addProduct")
//    @RequiresPermissions({"dsn:proBull:view"})
    public String tzrwList(Model model, String sheetId) {
        model.addAttribute("sheetId", sheetId);
        return "shipments/InstShipmentsPlan/addProductPage";
    }
    /**
     * 保存外购件发货明细
     */
    @ResponseBody
    @PostMapping("/addPurchList")
    //    @RequiresPermissions({"report:query:view"})
    public JsonResult addPurchList(InstShipmentsPurchasedDetail instShipmentsPurchasedDetail, String[] drawName, String[] drawNum, String[] unit, String[] location,String[] remark) {
        instShipmentsPlanService.savePurchList(instShipmentsPurchasedDetail.getSheetId(),drawName,drawNum,unit,location,remark);
        return jsonResult(1);
    }
    /**
     * 提交主单
     *
     * @param model
     * @return
     */
    @ResponseBody
    @PostMapping("/submit")
//    @RequiresPermissions({"dsn:proTasks:view"})
    public JsonResult submit(Model model,Integer id) {
        int sub= instShipmentsPlanService.updateByIdSubmit(id);
        return jsonResult(1);
    }
    /**
     * 删除主单
     *
     * @param model
     * @return
     */
    @ResponseBody
    @PostMapping("/delMain")
//    @RequiresPermissions({"dsn:proTasks:view"})
    public JsonResult delMain(Model model,Integer id) {
        int del= instShipmentsPlanService.updateByIdDel(id);
        return jsonResult(1);
    }
    /**
     * 提交
     *
     * @param model
     * @return
     */
    @ResponseBody
    @PostMapping("/comit")
    public JsonResult comit(Model model,Integer id) {
        int comit= instShipmentsPlanEditService.comit(id);
        return jsonResult(1);
    }
    /**
     * 提交
     *
     * @param model
     * @return
     */
    @ResponseBody
    @PostMapping("/regit")
    public JsonResult regit(Model model,Integer id) {
        int comit= instShipmentsPlanEditService.regit(id);
        return jsonResult(1);
    }
    /**
     * 制作件明细单
     *
     * @param model
     * @return
     */
    @ResponseBody
    @PostMapping("/delProduct")
//    @RequiresPermissions({"dsn:proTasks:view"})
    public JsonResult delProduct(Model model,Integer id) {
        int del= instShipmentsPlanService.updateByIdDelProduct(id);
        return jsonResult(1);
    }
    /**
     * 修改页面
     *
     * @param model
     * @return
     */
    @GetMapping("/listDetail/{id}")
    public String queryDetail(@PathVariable("id") Integer id, Model model) {
        InstShipmentsProductDetail instShipmentsProductDetail = new InstShipmentsProductDetail();
        instShipmentsProductDetail.setSheetId(id);
        List<InstShipmentsPurchasedDetail> list = instShipmentsPlanService.selectPurchDetail(instShipmentsProductDetail);
        model.addAttribute("instShipmentsPlanSheet",planSheetRepository.getOne(id));
        model.addAttribute("purchDetail", list);
        model.addAttribute("sheetId", id);
        return "shipments/InstShipmentsPlanEdit/detailPage";
    }
    /**
     * 查询明细
     *
     * @param model
     * @return
     */
    @GetMapping("/listDetailSelect/{id}")
    public String listDetailSelect(@PathVariable("id") Integer id, Model model) {
        InstShipmentsProductDetail instShipmentsProductDetail = new InstShipmentsProductDetail();
        instShipmentsProductDetail.setSheetId(id);
        List<InstShipmentsPurchasedDetail> list = instShipmentsPlanService.selectPurchDetail(instShipmentsProductDetail);
        model.addAttribute("instShipmentsPlanSheet",planSheetRepository.getOne(id));
        model.addAttribute("purchDetail", list);
        model.addAttribute("sheetId", id);
        return "shipments/InstShipmentsPlanEdit/detailPageSelect";
    }
}
